let CpikeId;
layui.use(['form','laydate', 'layer','table','laypage', 'element','upload'],function(){
    form = layui.form;
    layer = layui.layer;
    table = layui.table,
    laydate = layui.laydate;//日期
    upload = layui.upload;//上传
    element = layui.element; //元素操作
    laypage = layui.laypage; //分页
    CpikeId=GetQueryString("CpikeId");
    TypelistByTypeId(2);
    if(!isNotNull(CpikeId)){
        CpikeId=0;
        $('.add').show();
    }else{
        SelectCpilkeListById();
        $('.update').show();
    }
    selectAllProductInfoByInfoPc();
    //日期时间范围
    laydate.render({
        elem: '#CplikTime'
        , type: 'datetime'
        , range: true
        ,trigger: 'click'
    });
});
function searchInfo(){
    pageNo = 1
    selectAllProductInfoByInfoPc();
}
function submitInfo() {
    $.ajax({
        url: httpurl+"/ProductListByTypeIdInfo",
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        data: {
            "proIdList":listId.join(",")
        },
        success: function(data) {
            let dataList=data.data;
            let TitleInfo="";
            for(let i=0;i<dataList.length;i++) {
                TitleInfo+='<div class="mealList">' +
                    '<li class="margin_left2b prolist">' +
                    '<label class="check-box">' +
                    '<input type="checkbox" name="parentList" onclick="parentListFun(this);" value="'+dataList[i].id+'"/>' +
                    '<span>'+dataList[i].title+'</span>' +
                    '</label>' +
                    '&nbsp;&nbsp;&nbsp;秒杀数量：<input type="number" step="1" class="form-control form-boxed width10 personNum" value="0" step="1" placeholder="请输入数量" />' +
                    '&nbsp;&nbsp;&nbsp;已经秒杀数量(虚拟)：<input type="number" step="1" class="form-control form-boxed width10 alreadyNum" value="0" step="1" placeholder="请输入数量" /><br/>' +
                    '</li>';
                let dataInfo=dataList[i].setMealEntities;
                if(dataInfo!=null){
                    TitleInfo+='<li class="margin_left10b prochild">';
                    for(let a=0;a<dataInfo.length;a++){
                        TitleInfo+='<span class="meals"><label class="check-box">' +
                            '<input type="checkbox" name="childrenList" onclick="childrenListFun(this);" value="'+dataInfo[a].id+'"/>'+
                            '<span>'+dataInfo[a].meal+'</span>'+
                            '</label>'+
                            '&nbsp;&nbsp;&nbsp;秒杀价格：<input type="number" step="1" class="form-control form-boxed width10 cplikPrice" value="'+dataInfo[a].newprice+'" step="0.01" placeholder="请输入价格" />' +
                            '&nbsp;&nbsp;&nbsp;普通价格：<input readonly="readonly" type="number" class="form-control form-boxed width10 newprice" value="'+dataInfo[a].newprice+'" step="0.01" placeholder="请输入普通价格" /></span>'+
                            '</span><br/>';
                    }
                    TitleInfo+="</li></div>";
                }
            }
            $("#AllChooseList").html(TitleInfo);
        }
    });
}

//查询所有信息
function SelectCpilkeListById(){
    $.ajax({
        url: httpurl+"/findCpilkeIdpc",
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        data: {
            "id":CpikeId
        },
        success: function(data) {
            let dataInfo=data.data;
            $("#names").val(dataInfo.name);
            $("#proname").val(dataInfo.productEntity.title);
            $("#CplikTime").val(dataInfo.starttime+" - "+dataInfo.endtime);
            HandleCplikProductInfo(dataInfo);
        }
    });
}

//分页
function page(){
    laypage.render({
        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
        ,count:pageSize //数据总数，从服务端得到
        ,curr: pageNo //获取起始页 location.hash.replace('#!fenye=', '')
        ,hash: 'fenye' //自定义hash值
        ,layout: ['count', 'prev', 'page', 'next', 'skip']
        ,limit:15
        ,jump: function(obj, first){
            //首次不执行
            if(!first){
                //	  console.log("当前页==="+obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                //	  console.log("每页显示条数==="+obj.limit); //得到每页显示的条数
                pageNo=obj.curr;
                pageSize=obj.limit;
                selectAllProductInfoByInfoPc();
            }
        }
    });
}

//根据商品id查找商品的套餐
function HandleCplikProductInfo(dataList) {
    let TitleInfo='<div class="mealList">' +
            '<li class="margin_left2b prolist">' +
            '<label class="check-box">' +
            '<input type="checkbox" name="parentList" onclick="parentListFun(this);" value="'+dataList.productEntity.id+'"/>' +
            '<span>'+dataList.productEntity.title+'</span>' +
            '</label>' +
            '&nbsp;&nbsp;&nbsp;秒杀数量：<input type="number" step="1" class="form-control form-boxed width10 personNum" value="'+dataList.num+'" step="1" placeholder="请输入数量" />' +
            '&nbsp;&nbsp;&nbsp;已经秒杀数量(虚拟)：<input type="number" step="1" class="form-control form-boxed width10 alreadyNum" value="'+dataList.purchased+'" step="1" placeholder="请输入数量" /><br/>' +
            '</li>';
        let dataInfo=dataList.setMealEntity;
        if(dataInfo!=null){
            console.log(dataInfo.length);
            TitleInfo+='<li class="margin_left10b prochild">';
            for(let a=0;a<dataInfo.length;a++){
                TitleInfo+='<span class="meals"><label class="check-box">' +
                    '<input type="checkbox" name="childrenList" onclick="childrenListFun(this);" value="'+dataInfo[a].id+'"/>'+
                    '<span>'+dataInfo[a].meal+'</span>'+
                    '</label>'+
                    '&nbsp;&nbsp;&nbsp;秒杀价格：<input type="number" step="1" class="form-control form-boxed width10 cplikPrice" value="'+dataInfo[a].cpikePrice+'" step="0.01" placeholder="请输入价格" />' +
                    '&nbsp;&nbsp;&nbsp;普通价格：<input readonly="readonly" type="number" class="form-control form-boxed width10 newprice" value="'+dataInfo[a].newprice+'" step="0.01" placeholder="请输入普通价格" /></span>'+
                    '</span><br/>';
            }
            TitleInfo+="</li></div>";
        }
        $("#AllChooseList").html(TitleInfo);
}

function SubmitInfo(){
    let CplikTime = $("#CplikTime").val();//下单时间
    let startTime = CplikTime.split(" - ")[0];
    let endTime = CplikTime.split(" - ")[1];
    if(CplikTime==""){layer.alert("请选择开始时间!");return false;}
    let names=$("#names").val();
    if(names==""){layer.alert("请输入秒杀名称!");return false;}
    let prolist=$("#AllChooseList").find(".prolist");
    let proList="";
    for(let i=0;i<prolist.length;i++){
        let find1 = $(prolist[i]).find("input[name=parentList]");
        if(find1.is(":checked")){
            let proval = find1.val();
            let num=$(prolist[i]).find(".personNum").val();
            let alreadyNum=$(prolist[i]).find(".alreadyNum").val();
            let mealList="";
            let select_list = $(prolist[i]).parent().find(".prochild .meals");
            for(let a=0;a<select_list.length;a++){
                let find = $(select_list[a]).find("input[name=childrenList]");
                let cplikPrice = $(select_list[a]).find(".cplikPrice");
                if($(find).is(":checked")) {
                    let mealid= $(find).val();
                    let val= $(cplikPrice).val();
                    mealList+='{"mealid":'+mealid+',"val":'+val+'}'+",";
                }
            }
            if (mealList.length > 0) {
                mealList = mealList.substring(0, mealList.length - 1);
            }
            proList+='{"nameId":'+proval+',"num":'+num+',"meal":['+mealList+'],"alreadyNum":'+alreadyNum+'},';
        }
    }
    if(proList==""){
        layer.alert("请选择秒杀商品!");return false;
    }
    proList=proList.substring(0,proList.length-1);
    let proIdList="["+proList+"]";
    top.layer.confirm('是否确认提交,如果存在设置秒杀的商品,此次提交将覆盖之前秒杀商品的设置，是否继续？？',{icon: 3,title:'提示信息'},function(index){
        var index = top.layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        let HttpAjax="/updateCpikeInfo";
        if(CpikeId==0){
            HttpAjax="/insertCpikeInfo";
        }
        setTimeout(function(){
            $.ajax({
                type:"POST",
                url: httpurl+HttpAjax,
                cache: false,
                headers: {
                    "Access-Control-Allow-Origin": "*",
                    "Access-Control-Allow-Methods": "*",
                    "Access-Control-Allow-Headers": "Authorization",
                    "Authorization": GetTokenInfo()
                },
                contentType : 'application/json',
                data: JSON.stringify({
                    "id":CpikeId,
                    "names":names,
                    "startTime":startTime,
                    "endTime":endTime,
                    "proIdList":proIdList,
                }),
                success: function(data) {
                    layer.closeAll();
                    top.layer.msg(data.msg);
                    window.location.href="CpikeList.html";
                }
            });
        },1000);
    });
}
